package com.zlsoft.web.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.daimajia.numberprogressbar.NumberProgressBar;
import com.zenglb.downloadinstaller.DownloadInstaller;
import com.zenglb.downloadinstaller.DownloadProgressCallBack;
import com.zlsoft.mobilenurse.R;
import com.zlsoft.web.bean.CheckVersionResult;

/**
 * 升级对话框dialog
 */
public class UpdateAppDialog extends AlertDialog {
    private Context mContext;
    private TextView mUnConfirm, mContent, mBtnConfirm, mVersion, mTitle;
    private NumberProgressBar progressBar;
    private CheckVersionResult checkVersionResult;
    private MainActivity startActivityLauncher;

    public UpdateAppDialog(Context context, CheckVersionResult checkVersionResult, MainActivity startActivityLauncher) {
        super(context);
        this.mContext = context;
        this.checkVersionResult = checkVersionResult;
        this.startActivityLauncher = startActivityLauncher;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.getWindow().setBackgroundDrawable(new BitmapDrawable());
        setContentView(R.layout.update_app_alert);
        this.setCancelable(false);
        mVersion = findViewById(R.id.version);
        mUnConfirm = findViewById(R.id.btn_close);
        mContent = findViewById(R.id.content);
        mTitle = findViewById(R.id.title);
        progressBar = findViewById(R.id.tips_progress);
        mBtnConfirm = findViewById(R.id.btn_confirm);

//         mContent.setText(checkVersionResult.getDescription());
        mVersion.setText(checkVersionResult.getVersionName() + ":" + checkVersionResult.getVersionCode());

        if (checkVersionResult.getUpdateType() == 1) {
            mTitle.setText("强制更新");
            mUnConfirm.setText("退出应用");
        }

        mBtnConfirm.setOnClickListener(v -> {
            mBtnConfirm.setBackgroundResource(R.drawable.shape_btn_grey_bg);
            if (checkVersionResult.getUpdateType() == 1) {
                progressBar.setVisibility(View.VISIBLE);
                new DownloadInstaller(mContext, checkVersionResult.getPackageUrl(), true, new DownloadProgressCallBack() {
                    @Override
                    public void downloadProgress(int progress) {
                        Log.e("qqz", "Progress" + progress);
                    }

                    @Override
                    public void downloadException(Exception e) {
                        e.printStackTrace();
                    }


                    @Override
                    public void onInstallStart() {
                        Toast.makeText(mContext, "开始安装", Toast.LENGTH_SHORT).show();

                    }
                }).start();

            } else {
                dismiss();
                new DownloadInstaller(mContext, checkVersionResult.getPackageUrl(), startActivityLauncher).start();
                Toast.makeText(mContext, "后台下载升级中", Toast.LENGTH_LONG).show();
            }
        });

        mUnConfirm.setOnClickListener(v -> {
            if (checkVersionResult.getUpdateType() == 1) {
                System.exit(0);
            } else {
                dismiss();
            }
        });
    }
}